package gui;
import ttt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;

public class GraphicalBoard implements BoardUserInterface {
	
	public static void main(String[] args) {
		@SuppressWarnings("unused")
		BoardUserInterface b = new GraphicalBoard();
	}
	
	private TicTacToe t;
	private Field[][] fields = new Field[3][3];
	
	public GraphicalBoard() {
		JPanel panel = new JPanel(new GridLayout(3, 3));
		for(int i = 0; i < 3; i++) {
			for(int j = 0; j < 3; j++) {
				Field f = new Field();
				fields[i][j] = f;
				panel.add(f);
			}
		}
		
		JFrame frame = new JFrame("TicTacToe");
		frame.add(panel);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public void drawBoard() {
		int[][] board = t.getBoard();
		for(int i = 0; i < 3; i++) {
			for(int j = 0; j < 3; j++) {
				fields[i][j].setMark(board[i][j]);
			}
		}
	}
	
	public void setTicTacToeBoard(TicTacToe t) {
		this.t = t;
	}
	
	public void stateChanged(ChangeEvent event) {
		drawBoard();
	}
}
